/**
 * Copyright 厦门中软海晟信息技术有限公司 版权所有 违者必究 2019
 */
package com.leetCode;

/**
 * @author : sunzb(sunzb@hsit.com.cn)
 * @date: 2019/5/14
 */
// _33_121_BestTimeToBuyAndSellStock
public class _13_122_BestTimeToBuyAndSellStockII {

    public static int maxProfit1(int[] prices) {
        if (prices.length < 1) return 0;
        int i = 0;
        int valley;
        int peak;
        int maxprofit = 0;
        int len = prices.length - 1;
        while (i < len) {
            while (i < len && prices[i] >= prices[i + 1])
                i++;
            valley = prices[i];
            while (i < len && prices[i] <= prices[i + 1])
                i++;
            peak = prices[i];
            maxprofit += peak - valley;
        }
        return maxprofit;
    }

    public static int maxProfit(int[] prices) {
        int len = prices.length - 1;
        int sum = 0;
        for (int i = 0; i < len; i++) {
            if (prices[i + 1] > prices[i]) {
                sum += prices[i + 1] - prices[i];
            }
        }
        return sum;
    }

    public static void main(String[] args) {
        System.out.println(maxProfit1(new int[] {7,1,5,3,6,4}));
    }
}
